package tool;
import java.util.Base64;
public class Encryption {
    public String Encode(String text){
        String base64Encoded = base64Encode(text);
        String railFenceEncoded = railFenceEncrypt(base64Encoded, 2);
        return railFenceEncoded;
    }
    // Base64 Encoding
    private static String base64Encode(String input) {
        return Base64.getEncoder().encodeToString(input.getBytes());
    }

    // Rail Fence Cipher Encryption
    private static String railFenceEncrypt(String text, int rails) {
        StringBuilder[] rail = new StringBuilder[rails];
        for (int i = 0; i < rails; i++) {
            rail[i] = new StringBuilder();
        }

        boolean dirDown = false;
        int row = 0;

        for (char ch : text.toCharArray()) {
            rail[row].append(ch);
            if (row == 0 || row == rails - 1) {
                dirDown = !dirDown;
            }
            row += dirDown ? 1 : -1;
        }

        StringBuilder result = new StringBuilder();
        for (StringBuilder r : rail) {
            result.append(r);
        }
        return result.toString();
    }
}
